﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using TanashCMS.Business;

public partial class manager_inventory_CorrectItemKeys : CMSManagementPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FixKeys();
        FixOrderIndexes();
        Response.Write("<p><a href='default.aspx'>back to inventory</a></p>");
    }

    private void FixOrderIndexes()
    {
        Response.Write("Fixing order indexes...");
        Response.Flush();
        NHibernate.ITransaction tx = Configuration.GetCurrentSession().BeginTransaction();

        try
        {
            List<Category> categories = Category.List(mWebsite.ID,0);
            int i = 0;
            foreach (Category category in categories)
            {
                category.OrderIndex = i++;
                FixSubCategoryIndexes(category.ID);
                FixProductIndexes(category.ID);
                Configuration.GetCurrentSession().Save(category);
            }
            tx.Commit();
        }
        catch
        {
            Response.Write("failed! rolling back");
            tx.Rollback();
            throw;
        }
        Response.Write("finished!");
    }

    private void FixSubCategoryIndexes(int parentId)
    {
        List<Category> categories = Category.List(mWebsite.ID, parentId);
        int i = 0;
        foreach (Category category in categories)
        {
            category.OrderIndex = i++;
            FixSubCategoryIndexes(category.ID);
            FixProductIndexes(category.ID);
            Configuration.GetCurrentSession().Save(category);
        }
    }

    private void FixProductIndexes(int categoryId)
    {
        List<Product> products = Product.List(mWebsite.ID, categoryId);
        int i = 0;
        foreach (Product product in products)
        {
            product.OrderIndex = i++;
            Configuration.GetCurrentSession().Save(product);
        }
    }

    private void FixKeys()
    {
        Response.Write("Fixing Keys...");
        Response.Flush();
        NHibernate.ITransaction tx = Configuration.GetCurrentSession().BeginTransaction();

        try
        {
            List<Product> products = Product.List(mWebsite.ID);
            foreach (Product product in products)
            {
                product.ItemKey = CMSDataObject.GetKey(product.Title);
                Configuration.GetCurrentSession().Save(product);
            }
            List<Category> categories = Category.List(mWebsite.ID, true);
            foreach (Category category in categories)
            {
                category.ItemKey = CMSDataObject.GetKey(category.Title);
                Configuration.GetCurrentSession().Save(category);
            }
            List<WebPage> webpages = WebPage.List(mWebsite.ID, true);
            foreach (WebPage webpage in webpages)
            {
                webpage.ItemKey = CMSDataObject.GetKey(webpage.Title);
                Configuration.GetCurrentSession().Save(webpage);
            }

            IList<Navigation> navigations = Navigation.ListAll(mWebsite.ID);
            foreach (Navigation navigation in navigations)
            {
                navigation.ItemKey = CMSDataObject.GetKey(navigation.Title);
                Configuration.GetCurrentSession().Save(navigation);
            }
            tx.Commit();
        }
        catch
        {
            Response.Write("failed!! Rolling back<br/>");
            tx.Rollback();
            throw;
        }
        Response.Write("finished.<br/>");
    }
}
